<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :error.sync='error'
      error-text='加载失败，请点击重试'
    >
      <van-cell v-for="(article, index) in list" :key="index" :title="article.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search'
export default {
  name: 'SearchResult',
  components: {},
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      perpage: 20,
      error: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
   async onLoad () {
     try {
        // 1. 请求获取数据
        const { data } = await getSearchResult({
          page: this.page, // 页码
          per_page: this.perpage, // 每页大小
          q: this.searchText // 查询关键词
        })
        // 2. 将数据添加到列表中
      const { results } = data.data
      this.list.push(...results)
       // 3. 设置加载状态结束
       this.loading = false
       // 4. 判断数据是否加载完毕
       if (results.length) {
        //  有就获取下一页
        this.page++
       } else {
        //  每有就把加载状态设置为true
        this.finished = true
       }
     } catch (error) {
       this.error = true
       this.loading = false
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
